//
//  MZNetHandle.m
//  AFN封装_0818
//
//  Created by 孟镇 on 15/10/20.
//  Copyright © 2015年 老孟. All rights reserved.
//

#import "MZNetHandle.h"
#import "AFNetworking.h"
@implementation MZNetHandle


// GET请求

+(void)getUrl:(NSString *)url cookie:(NSString *)cookie result:(JSONBLOCK)block
{
    //NSString *newStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 返回数据的格式
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    if (cookie) {
        // 接口带Cookie
        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    }
    
    //手机端发送过来的数据 电脑端收不到
    [manager.requestSerializer setValue:@"BreadTrip/6.1.1/zh (iPhone6,2; iPhone OS8.0.2; zh-Hans zh_CN) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       
        // 如果请求成功, 回调请求到的数据, 同时在这里做本地缓存
        
        // 文件名
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
        // 沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        // 在这里读取本地缓存
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        block(result);
        
    }];    
}


// POST请求

+ (void)postUrl:(NSString *)url parame:(id)parame cookie:(NSString *)cookie result:(JSONBLOCK)block
{
    //NSString *newStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 返回数据格式
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    if (cookie) {
        // 接口带Cookie
        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    }
    
    [manager POST:url parameters:parame constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        //NSLog(@"error: %@", error);
    }];
    
    
}







@end
